package com.example.player.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.SeekBar;
import android.widget.TextClock;
import android.widget.TextView;

import com.easefun.polyvsdk.PolyvBitRate;
import com.easefun.polyvsdk.PolyvDownloader;
import com.easefun.polyvsdk.video.IMediaController;
import com.easefun.polyvsdk.video.PolyvVideoView;
import com.easefun.polyvsdk.video.listener.IPolyvOnGestureSwipeLeftListener;
import com.example.player.R;
import com.example.player.controllerView.PlayerMediaController;
import com.example.player.controllerView.PlayerProgressView;

public class PlayerActivity extends AppCompatActivity {
    private static final String TAG = PlayerActivity.class.getSimpleName();
    private PolyvVideoView videoView=null;
    private PlayerProgressView progressView = null;
    //private PlayerMediaController mediaController=null;

    private Button play_btn;
    private Button pause_btn;
    private Button stop_btn;
    private SeekBar seekBar;
    private TextView curtime;
    private TextView tottime;
    private PlayerMediaController mMediaController=null;
    private int mprogress;
    private Thread thread;
    private int fastForwardPos = 0;
    private boolean isPlaying;
    private int getPro = 0;

    protected void videoPause(){
        if(videoView.isPlaying()){
            videoView.pause();
        }
    }
    protected void videoStop(){
        if(videoView.isPlaying()){
            videoView.seekTo(0);
            videoPause();
        }
    }
    private void findIdAndNew() {
        progressView=(PlayerProgressView) findViewById(R.id.player_progress_view);
        play_btn=(Button)findViewById(R.id.play_btn);
        pause_btn=(Button)findViewById(R.id.pause_btn);
        stop_btn=(Button)findViewById(R.id.stop_btn);
        seekBar=(SeekBar)findViewById(R.id.seekBar);
        curtime=(TextView)findViewById(R.id.current_time);
        tottime=(TextView)findViewById(R.id.total_time);
    }
    public void initPlayer() {
        videoView = findViewById(R.id.polyv_video_view);
        videoView.setVid("04a59ede7540a0ffaf4b48122386b779_0");
        videoView.setAutoPlay(false);
        videoView.setOpenPreload(true);
        videoView.setBufferTimeoutSecond(true, 30);
        videoView.setMaxCacheDuration(10);
        //04a59ede7540a0ffaf4b48122386b779_0 罗小黑
        //04a59ede755210826265575b4b96e13a_0 伍陆柒
    }
    public void playMethod(){
        videoView.start();
        videoView.requestFocus();
        updateTime(curtime,videoView.getCurrentPosition());
        updateTime(tottime,videoView.getDuration());
        seekBar.setProgress(0);
        seekBar.setMax(0);
        seekBar.setMax(videoView.getDuration()/1000);
        Log.d(TAG, "onClick: vv.getDuration()"+videoView.getDuration());
        //更新进度条的
        new Thread() {
            @Override
            public void run() {
                try {
                    isPlaying = true;
                    while ((videoView.getCurrentPosition()<=videoView.getDuration())&&isPlaying) {
                        // 如果正在播放，0.5秒/次更新一次进度条
                        int current = videoView.getCurrentPosition();
                        seekBar.setProgress(current/1000);
                        updateTime(curtime,videoView.getCurrentPosition());
                        Log.i(TAG, "run: doing updateTime+++++++++++++++++++++++++++++"+current);
                        if(videoView.getCurrentPosition()==videoView.getDuration()){
                            isPlaying=false;
                            videoView.suspend();
                        }
                        sleep(500);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);
        findIdAndNew();
        initPlayer();

        Log.d(TAG, "onCreate: vv.getDuration()========"+videoView.getDuration());
        //给进度条设置最大的条长度
        //seekBar.setProgress(videoView.getDuration()/1000);
        //seekBar监听的部分
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mprogress=progress;
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                isPlaying=false;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int vv_progress=videoView.getDuration()/1000;
                int vv_current=videoView.getCurrentPosition()/1000;
                Log.e(TAG, "onStopTrackingTouch: -----------------------------mprogress="+mprogress );
                videoView.seekTo(mprogress*1000);
                updateTime(curtime,videoView.getCurrentPosition());
                isPlaying=true;
                playMethod();
            }
        });

        play_btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                playMethod();
            }
        });
        pause_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view){
                videoPause();
            }
        });

        stop_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                videoStop();
                isPlaying=false;
            }
        });
    }

    //update time
    private void updateTime(TextView textView, int millisecond){
        int second = millisecond/1000;
        int hh = second / 3600;
        int mm = second % 3600 / 60;
        int ss = second % 60;

        String str = null;
        if(hh!=0){
            str = String.format("%02d:%02d:%02d",hh,mm,ss);
        }else {
            str = String.format("%02d:%02d",mm,ss);
        }

        textView.setText(str);
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        if(videoView!=null){
            isPlaying=false;
            videoView.suspend();
            Log.d(TAG, "onDestroy() called and the resource occupied by videoView is released");
        }
    }
}